<#
    .AUTHOR
        cw50406@imcnam.ssmb.com  
    .GROUP
        RPA AA STE Support Team(dl.gt.cn.cstc.rpa.aa.ste@imcnam.ssmb.com)
    .VERSION
        2.0
    .Synopsis
        Close RDC Sessions
    .DESCRIPTION
        Close all the other rdcmon processes with only latest one started by current user
    .EXAMPLE
        .\close_old_rdc_process.ps1 c:\temp\test.log 12:23
        close old RDC Sessions with log file 'c:\temp\test.log' and autosysjob start time "12:23"
    .Update
        09/11/2017--optimize code with more clear comments structure
        08/29/2017--optimize email sending code with domain,env,node,running result ...
        08/17/2017--send notifications email to team
        08/01/2017--Optimize code structure
#>
param(
	[Parameter(Mandatory=$True)]   
	[string]$log,                                       # the log file to write log
	[Parameter(Mandatory=$True)]     
	[string]$start_time                                 # the starttime of autosysjob
)
#<---------------------------------- initilize functions and parameters
$this_user=$env:username                                              # the user which is running this script
$this_path = Split-Path -Parent ($MyInvocation.MyCommand.Definition)  # the full directory path of this script
. "$this_path\functions.ps1"                                          # invoke all the fucntions to the powershell script
#<---------------------------------- check the env to tell which user to run
If( ($rdcmon_process=Get-Process rdcmon -ea 0 |sort starttime) -ne $null ){
	For( $n=0; $n -lt ($rdcmon_process.count - 1); $n++ ){
		$rdcmon_process[$n]|kill -Force
	}
}else{Stop-WithError ": Not found the rdcmon.exe process" $log $this_user $start_time}
